package com.srwl.mytx.adapter;


import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.domain.MyFile;
import com.srwl.mytx.widget.CheckView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CoverSelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "CoverSelectAdapter";
    private final int screenWidth;

    private List<Bitmap> myFileList;
    private Context context;
    private InteractionListener listener;


    public CoverSelectAdapter(Context context, List<Bitmap> myFileList, InteractionListener listener) {
        this.myFileList = myFileList;
        this.context = context;
        this.listener = listener;
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cover_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.itemView.setFocusable(true);
        Bitmap cover = myFileList.get(position);
        ViewGroup.LayoutParams layoutParams = viewHolder.iv_cover.getLayoutParams();
        layoutParams.width = screenWidth / 10;
        Glide.with(context).load(cover).diskCacheStrategy(DiskCacheStrategy.ALL).into(viewHolder.iv_cover);


    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {

        if (myFileList.size() == 0) {
            return 1;
        }
        return myFileList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_cover)
        ImageView iv_cover;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onItemClick(CheckView checkView, int position);
    }

}